package service

import (
	"ginim/models"
	"ginim/utils"
	"github.com/gin-gonic/gin"
	"strconv"
)

func CreateCommunity(ctx *gin.Context) {
	ownerId, _ := strconv.Atoi(ctx.PostForm("owner_id"))
	name := ctx.PostForm("name")
	avatar := ctx.PostForm("avatar")
	description := ctx.PostForm("description")
	community := models.Community{}
	community.OwnerId = uint(ownerId)
	community.Name = name
	community.Avatar = avatar
	community.Description = description

	code, msg := models.CreateCommunity(community)
	if code == 0 {
		utils.ResponseSuccess(ctx.Writer, code, msg)
	} else {
		utils.ResponseFailure(ctx.Writer, msg)
	}
}

func CommunityList(ctx *gin.Context) {
	ownerId, _ := strconv.Atoi(ctx.PostForm("owner_id"))
	communityList := models.FindCommunityList(uint(ownerId))
	utils.ResponseListData(ctx.Writer, communityList, len(communityList))
}

func CommunityJoin(ctx *gin.Context) {
	userId, _ := strconv.Atoi(ctx.PostForm("user_id"))
	data := ctx.PostForm("data")

	code, msg := models.CommunityJoin(uint(userId), data)
	if code == 0 {
		utils.ResponseSuccess(ctx.Writer, code, msg)
	} else {
		utils.ResponseFailure(ctx.Writer, msg)
	}
}
